<template> 
  <div class="app-container">
          <el-form :inline="true" ref="queryForm" :model="listQuery" size="small"  label-width="140px">
            <el-form-item label="输入搜索：">
              <el-input style="width: 203px" v-model="listQuery.name" placeholder="品牌名称/关键字"></el-input>
            </el-form-item>
             <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="searchBrandList">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                  </el-form-item>
          </el-form>
        <el-row :gutter="10" class="mb8">
             <el-col :span="1.5">
               <el-button
                 type="primary"
                 icon="el-icon-plus"
                 size="mini"
                 @click="handleAdd"
                 v-hasPermi="['sys:role:create']"
               >新增</el-button>
             </el-col>
             <el-col :span="1.5">
               <el-button
                 type="success"
                 icon="el-icon-edit"
                 size="mini"
                 :disabled="single"
                 @click="handleUpdate"
                 v-hasPermi="['sys:role:update']"
               >修改</el-button>
             </el-col>
             <el-col :span="1.5">
               <el-button
                 type="danger"
                 icon="el-icon-delete"
                 size="mini"
                 :disabled="multiple"
                 @click="handleDelete"
                 v-hasPermi="['sys:role:delete']"
               >删除</el-button>
             </el-col>
             <el-col :span="1.5">
               <el-button
                 type="warning"
                 icon="el-icon-download"
                 size="mini"
                 @click="handleExport"
                 v-hasPermi="['system:post:export']"
               >导出</el-button>
             </el-col>
           </el-row>

      <el-table ref="brandTable"
                :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                v-loading="listLoading"
                border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="品牌名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="品牌首字母" width="100" align="center">
          <template slot-scope="scope">{{scope.row.firstLetter}}</template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{scope.row.sort}}</template>
        </el-table-column>
        <el-table-column label="品牌制造商" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleFactoryStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.factoryStatus">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleShowStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.showStatus">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="相关" width="220" align="center">
          <template slot-scope="scope">
            <span>商品：</span>
            <el-button
              size="mini"
              type="text"
              @click="getProductList(scope.$index, scope.row)">100
            </el-button>
            <span>评价：</span>
            <el-button
              size="mini"
              type="text"
              @click="getProductCommentList(scope.$index, scope.row)">1000
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

    <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>
    <div class="pagination-container">

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
     <!-- 添加或修改角色配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px">
          <el-form :model="brand" :rules="rules" ref="brandFrom" label-width="150px">
                <el-form-item label="品牌名称：" prop="name">
                  <el-input v-model="brand.name"></el-input>
                </el-form-item>
                <el-form-item label="品牌首字母：">
                  <el-input v-model="brand.firstLetter"></el-input>
                </el-form-item>
                <el-form-item label="品牌LOGO：" prop="logo">
                  <single-upload v-model="brand.logo"></single-upload>
                </el-form-item>
                <el-form-item label="品牌专区大图：">
                  <single-upload v-model="brand.bigPic"></single-upload>
                </el-form-item>
                <el-form-item label="品牌故事：">
                  <el-input
                    placeholder="请输入内容"
                    type="textarea"
                    v-model="brand.brandStory"
                    :autosize="true"></el-input>
                </el-form-item>
                <el-form-item label="排序：" prop="sort">
                  <el-input v-model.number="brand.sort"></el-input>
                </el-form-item>
                <el-form-item label="是否显示：">
                  <el-radio-group v-model="brand.showStatus">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="品牌制造商：">
                  <el-radio-group v-model="brand.factoryStatus">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="onSubmit('brandFrom')">提交</el-button>
                  <el-button v-if="!isEdit" @click="resetForm('brandFrom')">重置</el-button>
                </el-form-item>
              </el-form>
        </el-dialog>
    </div>
  </div>
</template>
<script>
import SingleUpload from '@/components/Upload/singleUpload'
  import {fetchList, updateShowStatus, updateFactoryStatus, deleteBrand,createBrand, getBrand, updateBrand} from '@/api/pms/brand'
const defaultBrand={
    bigPic: '',
    brandStory: '',
    factoryStatus: 0,
    firstLetter: '',
    logo: '',
    name: '',
    showStatus: 0,
    sort: 0
  };
  export default {
    name: 'brandList',
     components:{SingleUpload},
    data() {
      return {
      brand:Object.assign({}, defaultBrand),
              rules: {
                name: [
                  {required: true, message: '请输入品牌名称', trigger: 'blur'},
                  {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
                ],

                sort: [
                  {type: 'number', message: '排序必须为数字'}
                ],
              },
        // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
              // 总条数
             total: 0,
             isEdit:false,
              // 弹出层标题
                   title: "",
            // 是否显示弹出层
              open: false,
        operates: [

        ],
        operateType: null,
        listQuery: {
          keyword: null,
          pageNum: 1,
          pageSize: 10
        },
        list: null,

        listLoading: true,
        multipleSelection: []
      }
    },
    created() {
      this.getList();
    },
    methods: {
     /** 重置按钮操作 */
        resetQuery() {
          this.dateRange = [];
          this.resetForm("queryForm");
          this.handleQuery();
        },
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
          this.totalPage = response.data.pages;
          this.pageSize = response.data.size;
        });
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除品牌编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteBrand(ids).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });
      },

      handleFactoryStatusChange(index, row) {
        var data = new URLSearchParams();
        data.append("ids", row.id);
        data.append("factoryStatus", row.factoryStatus);
        updateFactoryStatus(data).then(response => {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        }).catch(error => {
          if (row.factoryStatus === 0) {
            row.factoryStatus = 1;
          } else {
            row.factoryStatus = 0;
          }
        });
      },
      handleShowStatusChange(index, row) {
        let data = new URLSearchParams();
        ;
        data.append("ids", row.id);
        data.append("showStatus", row.showStatus);
        updateShowStatus(data).then(response => {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        }).catch(error => {
          if (row.showStatus === 0) {
            row.showStatus = 1;
          } else {
            row.showStatus = 0;
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      searchBrandList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleBatchOperate() {
        console.log(this.multipleSelection);
        if (this.multipleSelection < 1) {
          this.$message({
            message: '请选择一条记录',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let showStatus = 0;
        if (this.operateType === 'showBrand') {
          showStatus = 1;
        } else if (this.operateType === 'hideBrand') {
          showStatus = 0;
        } else {
          this.$message({
            message: '请选择批量操作类型',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        let data = new URLSearchParams();
        data.append("ids", ids);
        data.append("showStatus", showStatus);
        updateShowStatus(data).then(response => {
          this.getList();
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        });
      },
      /** 新增按钮操作 */
         handleAdd() {
           this.reset();

           this.open = true;
           this.title = "添加角色";
         },
         /** 修改按钮操作 */
         handleUpdate(row) {
           this.reset();
           this.isEdit=true;
           const id = row.id || this.ids

           getBrand(id).then(response => {
             this.brand = response.data;
             this.open = true;
             this.title = "修改角色";
           });
         },
          /** 导出按钮操作 */
             handleExport() {
               const queryParams = this.queryParams;
               this.$confirm('是否确认导出所有角色数据项?', "警告", {
                   confirmButtonText: "确定",
                   cancelButtonText: "取消",
                   type: "warning"
                 }).then(function() {
                   return exportRole(queryParams);
                 }).then(response => {
                   this.download(response.msg);
                 }).catch(function() {});
             },
          // 取消按钮
             cancel() {
               this.open = false;
               this.reset();
             },
               // 多选框选中数据
                 handleSelectionChange(selection) {
                   this.ids = selection.map(item => item.id)
                   this.single = selection.length!=1
                   this.multiple = !selection.length
                 },
             /** 重置按钮操作 */
                 resetQuery() {
                   this.dateRange = [];
                   this.resetForm("queryForm");
                   this.handleQuery();
                 },
                  // 表单重置
                     reset() {

                       this.brand = {
                         id: undefined,
                         name: undefined,

                         sort: 0,
                         status: "0",

                         description: undefined
                       };
                       this.resetForm("form");
                     },
                      onSubmit(formName) {
                             this.$refs[formName].validate((valid) => {
                               if (valid) {
                                 this.$confirm('是否提交数据', '提示', {
                                   confirmButtonText: '确定',
                                   cancelButtonText: '取消',
                                   type: 'warning'
                                 }).then(() => {
                                   if (this.isEdit) {
                                     updateBrand(this.$route.query.id, this.brand).then(response => {

                                       this.$message({
                                         message: '修改成功',
                                         type: 'success',
                                         duration:1000
                                       });
                                       this.$router.back();
                                     });
                                   } else {
                                     createBrand(this.brand).then(response => {

                                       this.brand = Object.assign({},defaultBrand);
                                       this.$message({
                                         message: '提交成功',
                                         type: 'success',
                                         duration:1000
                                       });
                                     });
                                   }
                                 });

                               } else {
                                 this.$message({
                                   message: '验证失败',
                                   type: 'error',
                                   duration:1000
                                 });
                                 return false;
                               }
                             });
                           },
                           resetForm(formName) {

                             this.brand = Object.assign({},defaultBrand);
                           }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>


</style>


